package rcproxy

import (
	"github.com/gorilla/websocket"
	"sync"
)

type Shellr struct {
	Name  string
	conn  *websocket.Conn
	mutex sync.Mutex
}

func (s *Shellr) Call(req []byte) ([]byte, error) {
	//	加锁，避免多个 Runner 同时发起调用
	s.mutex.Lock()
	defer s.mutex.Unlock()

	//	写请求
	err := s.conn.WriteMessage(websocket.TextMessage, req)
	if err != nil {
		return nil, err
	}

	//	读应答，并返回
	_, rep, err := s.conn.ReadMessage()
	return rep, err
}

func NewShellr(name string, conn *websocket.Conn) *Shellr {
	s := new(Shellr)
	s.conn = conn
	s.Name = name
	return s
}
